<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-page-header @back="back" :content="content">
            </el-page-header>
            <el-form ref="form" class="edit-el-form"  :model="form" :rules="rules" label-width="auto">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" required></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model.number="form.sort" required>
                      <template slot="append">注：排序越小越靠前，默认100</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="按钮图标" prop="icon">
                    <Upload :limit="1" :file_list="icon_list" :catch_urls="catch_urls" :remove_urls="remove_urls" :upload_tip="upload_tip"></Upload>
                </el-form-item>
                <!--查看不展示按钮-->
                <el-form-item v-if="edit_type!=0">
                    <el-button type="primary" @click="submitForm('form')" :loading="loading">{{button_name}}</el-button>
                    <el-button @click="back">取消</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script>
    import { getTypeInfo,editType } from '@/api/type.js'
    import Upload from '@/components/Upload.vue';
    export default {
        name:'TypeEdit',
        components:{ Upload },
        data() {
            return {
                form: {
                    name: '',
                    icon:'',
                    sort: 100,
                },
                //校验规则
                rules:{
                    name: [
                        { required:true , message:'请输入分类名称',trigger:'blur' }
                    ],
                    sort:[
                        { required:true , message:'请输入排序号',trigger:'blur' },
                        { type:'number' , message:'排序号必须为数字'}
                    ],
                    icon:[
                        { required:true , message:'请上传图标'}
                    ]
                },
                //加载组件
                loading: false,
                button_name:'立即提交',
                content:'分类添加',
                //icon
                icon_list:[],
                upload_tip:'只能上传jpg/png/gif文件，且不超过500kb，仅允许上传一张',
            }
        },
        methods: {
            submitForm(formName){
                let that = this;
                    this.$refs[formName].validate((valid) => {
                            if (valid) {
                                this.loading=true;
                                this.button_name='正在提交';
                                var send_data={
                                    name:this.form.name,
                                    icon:this.form.icon,
                                    sort:this.form.sort,
                                };
                                if(this.id!=-1){
                                    send_data.id=this.id;
                                }
                                editType(send_data).then(function(response){
                                    let code=response.data.code;
                                    let type=code==1?'success':'error';
                                    that.$message({
                                        message:response.data.msg,
                                        type:type,
                                        onClose:function(e){
                                            that.loading=false;
                                            if(code==1){
                                               that.$router.push('/news/typelist');
                                            }else{
                                               that.button_name='立即提交';
                                            }
                                        }
                                    });
                                })
                            }else{
                                setTimeout(()=>{
                                  const is_error=document.getElementsByClassName('is-error');
                                  is_error[0].querySelector('input').focus();
                                },100);
                                return false;
                            }
                    })
            },
            back(){
                this.$router.go(-1);
            },
            //富文本内容
            catchData(e){
               this.form.content=e;
            },
            //这边只有一张，如果有数组就移除数组中的下标
            remove_urls(remove_index){
              if(remove_index==0){
                 this.form.icon=''
              }
            },
            //获取组件返回的图片路径
            catch_urls(urls){
               this.form.icon=urls[0];
            }
        },
        created(){
            if(this.id!=-1){
                var send_data={
                    id:this.id
                }
                let that=this;
                getTypeInfo(send_data).then(function(response){
                    if(response.data.code==1){
                        let val=response.data.info;
                        that.form.name=val.name;
                        that.form.sort=val.sort;
                        that.form.icon=val.icon;
                        if(val.icon!=null && val.icon.length>0){
                          that.icon_list=[{
                              name:0,
                              url:val.icon
                          }];
                        }
                    }
                })
            }
            //修改标题
            if(this.edit_type==1){
                this.content='分类添加';
            }else if(this.edit_type==0){
                this.content='分类详情';
            }else if(this.edit_type==2){
                this.content='分类编辑';
            }
        },
        computed:{
            id(){
                return this.$route.params.id;
            },
            edit_type(){
                return this.$route.params.edit_type;
            }
        }
    }
</script>

<style>
    .edit-el-form{
        margin-top:30px;
    }
</style>
